package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.future.enums.StatusCode;
import com.future.po.DrivingDriver;
import com.future.po.DrivingStatis;
import com.future.po.PublicStatis;
import com.future.po.User;
import com.future.service.DrivingDriverService;
import com.future.service.DrivingStatisService;
import com.future.service.PublicStatisService;
import com.future.utils.WebUtil;
import com.future.vo.DayActionVo;
import com.future.vo.DrivingDriverVo2;
import com.future.vo.ResponseResultVo;
import com.future.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


@Api(tags = "用户的API接口")
@RestController
@CrossOrigin
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private PublicStatisService publicStatisService;

    @Resource
    private DrivingDriverService drivingDriverService;


    @ApiOperation(value = "添加用户", notes = "根据User对象添加用户")
    @PostMapping
    public ResponseResultVo postUser(@RequestBody User user) {
        userService.save(user);
        return ResponseResultVo.success();
    }

    @ApiOperation(value = "删除用户", notes = "根据url中的userId删除用户")
    @DeleteMapping("/{userId}")
    public ResponseResultVo deleteUserById(@PathVariable Integer userId) {
        userService.removeById(userId);
        return ResponseResultVo.success();
    }

    @ApiOperation(value = "更新用户", notes = "根据User对象中的id更新用户")
    @PutMapping
    public ResponseResultVo putUser(@RequestBody User user) {
        userService.update(user, new UpdateWrapper<User>().eq("id", user.getId()));
        return ResponseResultVo.success();
    }

    /**
     * 返回个人违规记录
     * @param scene
     * @param userId
     * @return
     */
    @GetMapping("/statis/{scene}")
    public ResponseResultVo getStatis(@PathVariable String scene, @RequestParam Integer userId) {
        switch (scene) {
            case "public":
                PublicStatis publicStatis = publicStatisService.getOne(new QueryWrapper<PublicStatis>().eq("user_id", userId));
                return ResponseResultVo.success(publicStatis);
            case "driving":
                LocalDateTime ldt = LocalDateTime.of(LocalDateTime.now().getYear(), LocalDateTime.now().getMonth(), LocalDateTime.now().getDayOfMonth(), 0, 0,0);
                List<DrivingDriverVo2> list = drivingDriverService.statisticBehaveByDate(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME), ldt.plusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),null);
                if(list == null || list.isEmpty()){
                    return ResponseResultVo.success(null);
                }
                DayActionVo dav = new DayActionVo();
                dav.setCellphone(list.get(0).getCellphone());
                dav.setEyesClosed(list.get(0).getEyesClosed());
                dav.setHeadLowered(list.get(0).getHeadLowered());
                dav.setBothHandsLeavingWheel(list.get(0).getBothHandsLeavingWheel());
                dav.setSmoke(list.get(0).getSmoke());
                dav.setYawning(list.get(0).getYawning());
                dav.setNoFaceMask(list.get(0).getNoFaceMask());
                dav.setNotBucklingUp(list.get(0).getNotBucklingUp());
                dav.setNotFacingFront(list.get(0).getNotFacingFront());
                return ResponseResultVo.success(dav);
            default:
                return ResponseResultVo.failure(StatusCode.BAD_REQUEST, "不存在应用场景" + scene);
        }
    }
}
